/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using DaveSexton.DocProject.Resources;
using Microsoft.VisualStudio.CommandBars;

namespace DaveSexton.DocProject
{
	public sealed class ToolBarCollection : KeyedCollection<string, ToolBar>
	{
		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ToolBarCollection" /> class.
		/// </summary>
		public ToolBarCollection()
			: base(StringComparer.OrdinalIgnoreCase, -1)
		{
		}
		#endregion

		#region Methods
		protected override string GetKeyForItem(ToolBar item)
		{
			return item.Name;
		}

		protected override void InsertItem(int index, ToolBar item)
		{
			if (item == null)
				throw new ArgumentNullException("item");

			if (Items.Contains(item))
				throw new ArgumentException(string.Format(System.Globalization.CultureInfo.CurrentCulture,
					Errors.ToolBarExists, item.Name), "item");

			DocProjectEnvironment.RegisterToolBar(item);

			base.InsertItem(index, item);
		}

		protected override void SetItem(int index, ToolBar item)
		{
			if (item == null)
				throw new ArgumentNullException("item");

			ToolBar old = this.Items[index];

			if (old == item)
				return;

			if (Items.Contains(item))
				throw new ArgumentException(string.Format(System.Globalization.CultureInfo.CurrentCulture,
					Errors.ToolBarExists, item.Name), "item");

			DocProjectEnvironment.UnregisterToolBar(old);

			DocProjectEnvironment.RegisterToolBar(item);

			base.SetItem(index, item);
		}

		protected override void RemoveItem(int index)
		{
			ToolBar item = this.Items[index];

			DocProjectEnvironment.UnregisterToolBar(item);

			base.RemoveItem(index);
		}

		protected override void ClearItems()
		{
			foreach (ToolBar item in this.Items)
				DocProjectEnvironment.UnregisterToolBar(item);

			base.ClearItems();
		}
		#endregion
	}
}
